package william.list;

/**
 * @author ZhangShenao
 * @date 2024/1/22
 * @description <a href="https://leetcode.cn/problems/UHnkqh/">...</a>
 */
public class LCR_024_反转链表 {
    private class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode reverseList(ListNode head) {
        //边界条件校验
        if (head == null) {
            return head;
        }

        ListNode prev = null;
        ListNode next = null;
        ListNode cur = head;

        //从头开始遍历链表,反转指针指向
        while (cur != null) {
            //首先记录next节点
            next = cur.next;

            //修改指针方向
            cur.next = prev;

            //移动节点
            prev = cur;
            cur = next;
        }
        //最后返回prev为反转后的链表头结点
        return prev;
    }

}
